package com.byt.common.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.byt.base.utils.DisplayUtil;
import com.byt.common.R;

import androidx.annotation.ColorInt;

/**
 * 通用标题栏
 * 直接xml里面调用
 */
public class NormalTitleBar extends RelativeLayout {

    private RelativeLayout rlRight;
    private ImageView ivRight;
    private RelativeLayout rlBack;
    private ImageView ivBack;
    private TextView tvTitle, tvRight, tv_left;
    private RelativeLayout rlCommonTitle;
    private Context context;
    private View v_bar_normal_line;

    public NormalTitleBar(Context context) {
        this(context, null);
        this.context = context;
    }

    public NormalTitleBar(final Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        View.inflate(context, R.layout.bar_normal, this);
        rlBack = findViewById(R.id.rl_back);
        ivBack = findViewById(R.id.img_back);
        tvTitle = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        tv_left = findViewById(R.id.tv_left);
        ivRight = findViewById(R.id.image_right);
        rlRight = findViewById(R.id.rl_right);
        rlCommonTitle = findViewById(R.id.common_title);
        v_bar_normal_line = findViewById(R.id.v_bar_normal_line);
        //setHeaderHeight();
    }

    public void setHeaderHeight() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            rlCommonTitle.setPadding(0, DisplayUtil.getStatusBarHeight(context), 0, 0);
            rlCommonTitle.requestLayout();
        }
    }

    public void setNtbWhiteBg(boolean visibility) {
        rlCommonTitle.setBackgroundColor(Color.WHITE);
        tvTitle.setTextColor(Color.parseColor("#272727"));
        tvRight.setTextColor(Color.parseColor("#272727"));
        tv_left.setTextColor(Color.parseColor("#272727"));
        ivBack.setImageResource(R.drawable.ico_common_back_black);
        v_bar_normal_line.setVisibility(visibility ? VISIBLE : GONE);
    }

    public void setNtbRightTextColor(int color) {
        tvRight.setTextColor(color);
    }


    public void setNtbStaffWhiteBg(boolean visibility) {
        rlCommonTitle.setBackgroundColor(Color.WHITE);
        tvTitle.setTextColor(Color.parseColor("#464f66"));
        tvRight.setTextColor(Color.parseColor("#464f66"));
        tv_left.setTextColor(Color.parseColor("#464f66"));
        ivBack.setImageResource(R.drawable.icon_staff_back_black);
        v_bar_normal_line.setVisibility(visibility ? VISIBLE : GONE);
    }

    public void setNtbExpertWhiteBg(boolean visibility) {
        rlCommonTitle.setBackgroundColor(Color.WHITE);
        tvTitle.setTextColor(Color.parseColor("#0e0e0e"));
        TextPaint paintTitle = tvTitle.getPaint();
        paintTitle.setFakeBoldText(true);
        tvRight.setTextColor(Color.parseColor("#63d3c5"));
        tvRight.setTextSize(15f);
        TextPaint paintRight = tvTitle.getPaint();
        paintRight.setFakeBoldText(true);
        tv_left.setTextColor(Color.parseColor("#0e0e0e"));
        ivBack.setImageResource(R.drawable.icon_expert_back);
        v_bar_normal_line.setVisibility(visibility ? VISIBLE : GONE);
    }

    public void setNtbTransparentStyle(boolean visibility) {
        rlCommonTitle.setBackgroundColor(Color.TRANSPARENT);
        tvTitle.setTextColor(Color.parseColor("#272727"));
        tvRight.setTextColor(Color.parseColor("#272727"));
        tv_left.setTextColor(Color.parseColor("#272727"));
        ivBack.setImageResource(R.drawable.ico_common_back_black);
        v_bar_normal_line.setVisibility(visibility ? VISIBLE : GONE);
    }

    /**
     * 管理返回按钮
     */
    public void setBackVisibility(boolean visible) {
        if (visible) {
            rlBack.setVisibility(View.VISIBLE);
        } else {
            rlBack.setVisibility(View.GONE);
        }
    }

    /**
     * 管理标题
     */
    public void setTitleVisibility(boolean visible) {
        if (visible) {
            tvTitle.setVisibility(View.VISIBLE);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
    }

    public void setTitleText(String string) {
        tvTitle.setText(string);
    }

    public void setTitleText(int string) {
        tvTitle.setText(string);
    }

    public void setTitleColor(int color) {
        tvTitle.setTextColor(color);
    }

    public void setTitleMaxLength(int maxPixels) {
        tvTitle.setMaxWidth(maxPixels);
        tvTitle.setSingleLine();
        tvTitle.setEllipsize(TextUtils.TruncateAt.END);
    }

    /**
     * 右图标
     */
    public void setRightImagVisibility(boolean visible) {
        rlRight.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setRightImagSrc(int id) {
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(id);
    }

    public void setRightImagSelected(boolean imagSelected) {
        ivRight.setSelected(imagSelected);
    }

    public void setRightImagSrcNShow(int id) {
        ivRight.setImageResource(id);
    }

    /**
     * 获取右按钮
     *
     * @return
     */
    public ImageView getRightImage() {
        return ivRight;
    }

    /**
     * 左图标
     *
     * @param id
     */
    public NormalTitleBar setLeftImagSrc(int id) {
        ivBack.setImageResource(id);
        return this;
    }

    /**
     * 左图标
     *
     * @param
     */
    public NormalTitleBar setLeftImagPadding(int padding) {
        ivBack.setPadding(padding, padding, padding, padding);
        return this;
    }

    /**
     * 左图标
     *
     * @param visible
     */
    public void setLeftImagVisibility(boolean visible) {
        ivBack.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 左标题
     */
    public void setLeftTitleVisibility(boolean visible) {
        tv_left.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 右标题
     */
    public void setRightTitleVisibility(boolean visible) {
        tvRight.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public TextView getLeftTextView() {
        return tv_left;
    }

    public TextView getRightTextView() {
        return tvRight;
    }

    public void setRightTitle(String text) {
        tvRight.setText(text);
    }

    public void setLeftTitle(String text) {
        tv_left.setText(text);
    }

    public void setRightTitleEnabled(boolean enabled) {
        tvRight.setEnabled(enabled);
    }

    public void setLeftTitleEnabled(boolean enabled) {
        tv_left.setEnabled(enabled);
    }

    public void setLeftTitleEnabled(boolean enabled, @ColorInt int y_color, @ColorInt int n_color) {
        tv_left.setEnabled(enabled);
        tv_left.setTextColor(enabled ? y_color : n_color);
    }

    public void setRightTitleEnabled(boolean enabled, @ColorInt int y_color, @ColorInt int n_color) {
        tvRight.setEnabled(enabled);
        tvRight.setTextColor(enabled ? y_color : n_color);
    }

    /*
     * 点击事件
     */
    public void setOnBackListener(OnClickListener listener) {
        rlBack.setOnClickListener(listener);
    }

    public void setOnRightImagListener(OnClickListener listener) {
        rlRight.setOnClickListener(listener);
    }

    public void setOnLeftTextListener(OnClickListener listener) {
        tv_left.setOnClickListener(listener);
    }

    public void setOnRightTextListener(OnClickListener listener) {
        tvRight.setOnClickListener(listener);
    }

    /**
     * 标题背景颜色
     *
     * @param color
     */
    public void setBackGroundColor(int color) {
        rlCommonTitle.setBackgroundColor(color);
    }

    public Drawable getBackGroundDrawable() {
        return rlCommonTitle.getBackground();
    }


    public RelativeLayout getRlCommonTitle() {
        return rlCommonTitle;
    }
}